package codeTop.简单;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 挚爱之夕
 * @date 2021/9/28 - 09 - 28 - 21:03
 * @Description: codeTop.简单
 * @Version: 1.0
 */
public class _008_中序遍历 {
    /*
    94
    给定一个二叉树的根节点 root ，返回它的中序遍历,以列表形式返回。
     */
    private static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public static void main(String[] args) {
        TreeNode t4 = new TreeNode(4, null, null);
        TreeNode t3 = new TreeNode(2, t4, null);
        TreeNode t1 = new TreeNode(1, null, t3);
        List<Integer> list = inorderTraversal(t1);
        System.out.println(list);
    }

    private static List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();
        inorder(root, res);
        return res;
    }
    private static void inorder(TreeNode root, List<Integer> res) {
        if (root == null) {
            return;
        }
        inorder(root.left, res);
        res.add(root.val);
        inorder(root.right, res);
    }

}
